var navScroll = function(opt){
	 var opt = $.extend(this,{
            wrap : '#titlebar',
            bar : '.bar',
            active : '.active'
        },opt);
        this.init();
}
navScroll.prototype.init = function(){
	this.box = $(this.wrap);
    if(!this.box.length){
        return false;
    }
    this.bar = this.box.children(this.bar);
    this.a = this.bar.children();
    this.render();
}
navScroll.prototype.render = function(){
	this.initScrollLeftVal();
    this.selectCurChannel();
}
/**
 * 设置A标签的scroll-left值
 */
navScroll.prototype.initScrollLeftVal = function(){
    var body_w = document.body.clientWidth//body窗口宽度
        ,bar_w = this.bar[0].scrollWidth;//容器宽度
    this.a.each(function() {
        var s_l = 0,
            a_w = this.clientWidth,//内容宽度
            _offset = this.getBoundingClientRect().left;//获取元素位置,相当$(this).offset().left;
        s_l= body_w / 2 - a_w / 2 >= _offset ? 0 : _offset > bar_w - body_w / 2 - a_w / 2 ? Math.max(bar_w - body_w, 0) : -body_w / 2 + a_w / 2 + _offset,
        $(this).attr("scroll-left", s_l)
    })
}
/**
 * 选中项平移
 */
navScroll.prototype.selectCurChannel = function(){
    var _a = this.bar.children(this.active)
    var left = _a.attr("scroll-left");
    this.bar.scrollLeft(left)
}

$(document).ready(function(){
	new navScroll({wrap:'.titlebar_left',bar:'.titlebar',active:'on'});
})